package acs.offer.main;

import java.io.IOException;

import acs.offer.client.ClientInitializer;
import acs.offer.collection.Params;
import acs.offer.constant.EventType;
import acs.offer.controller.AcsController;
import acs.offer.controller.MessageQueue;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;


public class ClientRun {
    public static void main(String[] args) throws IOException {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ClientInitializer());
            Channel channel = b.connect("127.0.0.1", 8888).sync().channel();
            init();
//            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//            for (;;){
//                String line= in.readLine();
//                if(line== null){
//                    continue;
//                }
//                ClientHandler.sendMessage(Message.build(line));
//            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

    private static void init() {
        MessageQueue.getInstance().putMessage(EventType.QUERY_CACHE, Params.build());
        MessageQueue.getInstance().registerObserver(AcsController.getInstance());
        MessageQueue.getInstance().eventLoop();
    }
}
